package com.css.fxfzysx.modules.cztDataMange.controller;

import cn.hutool.core.io.FileUtil;
import com.css.fxfzysx.base.response.RestResponse;
import com.css.fxfzysx.modules.cztDataMange.entity.CbhdInfoEntity;
import com.css.fxfzysx.modules.cztDataMange.service.SdoBuildingsService;
import com.css.fxfzysx.modules.vabStructure.vo.CrsEntiretyQueryParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("sdoBuildings")
@Slf4j
public class SdoBuildingsController {

    @Autowired
    private SdoBuildingsService sdoBuildingsService;

    /**
     * 导入建筑物历史震害数据
     */
    @PostMapping("uploadHsoBuildings")
    public RestResponse uploadHsoBuildings(@RequestParam MultipartFile file) {
        if (!FileUtil.getSuffix(file.getOriginalFilename()).equals("xls") && !FileUtil.getSuffix(file.getOriginalFilename()).equals("xlsx")) {
            return RestResponse.fail("文件格式不支持!");
        }
        return sdoBuildingsService.uploadHsoBuildings(file);
    }

    /**
     * 建筑物震害信息修改
     */
    @PostMapping("updateBuildings")
    public RestResponse updateBuildings(@RequestBody CbhdInfoEntity cbhdInfoEntity) {
        return sdoBuildingsService.updateBuildings(cbhdInfoEntity);
    }

    /**
     * 建筑物震害信息删除
     */
    @GetMapping("deleteBuildingsById")
    public RestResponse deleteBuildingsById(String number) {
        return sdoBuildingsService.deleteBuildingsById(number);
    }

    /**
     * 建筑物震害信息分页查询
     */
    @GetMapping("queryBuildings")
    public RestResponse queryBuildings(CbhdInfoEntity cbhdInfoEntity, HttpServletRequest request) {
        return sdoBuildingsService.queryBuildings(cbhdInfoEntity, request);
    }

    /**
     *
     * 钢筋混凝土查询
     */
    @GetMapping("queryCrsBuildings")
    public RestResponse queryCrsBuildings(CrsEntiretyQueryParam cbhdInfoEntity, HttpServletRequest request) {
        return sdoBuildingsService.queryCrsBuildings(cbhdInfoEntity, request);
    }

    @GetMapping("deleteCrsById")
    public RestResponse  deleteCrsById(String calId){
        return null;

    }
    /**
     * 建筑物震害信息,根据id查询
     */
    @GetMapping("getBuildingsById")
    public RestResponse getBuildingsById(String buildingsId) {
        return sdoBuildingsService.getBuildingsById(buildingsId);
    }

    /**
     * 建筑物震害信息,详情页面
     */
    @GetMapping("getBuildingsByNumber")
    public RestResponse getBuildingsByNumber(String number) {
        return sdoBuildingsService.getBuildingsByNumber(number);
    }

    /**
     * 建筑物震害信息,表格修改
     */
    @PostMapping("updateBuildingsForm")
    public RestResponse updateBuildingsForm(@RequestBody List<CbhdInfoEntity> cbhList) {
        int result = 0;
        try {
            result = sdoBuildingsService.updateBuildingsForm(cbhList);
            if(result==-1){
                return RestResponse.fail("每行数据之和必须在大于等于0.99小于等于1.01");
            }
            return RestResponse.succeed("修改成功!");
        } catch (Exception e) {
            String errorMessage = "修改失败!";
            log.error(errorMessage,e);
            return RestResponse.fail(errorMessage);
        }
    }

    /**
     * 生成图片
     */
    @PostMapping("generatePictures")
    public RestResponse generatePictures(HttpServletRequest request) {
        try {
            return sdoBuildingsService.generatePictures(request);
        } catch (IOException e) {
            String errorMessage = "图片下载失败!";
            log.error(errorMessage,e);
            return RestResponse.fail(errorMessage);
        }
    }
}
